package com.newcapec.erp.ui;

import com.newcapec.erp.bean.InventoryEntity;
import com.newcapec.erp.main.DataInit;
import com.newcapec.erp.utils.XMLFReader;
import com.newcapec.erp.utils.XMLFWriter;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class AddInventoryUI extends JFrame {
    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;

    /**
     * Create the frame.
     */
    public AddInventoryUI() {
        setIconImage(Toolkit.getDefaultToolkit().getImage("images/logo.png"));
        setTitle("新增库存");
        setBounds(100, 100, 511, 495);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel label = new JLabel("请输入"+ DataInit.inventoryColumnNames.get(0)+"：");
        label.setHorizontalAlignment(SwingConstants.RIGHT);
        label.setBounds(110, 21, 107, 33);
        contentPane.add(label);

        JLabel label_1 = new JLabel("请输入"+DataInit.inventoryColumnNames.get(1)+"：");
        label_1.setHorizontalAlignment(SwingConstants.RIGHT);
        label_1.setBounds(99, 75, 107, 33);
        contentPane.add(label_1);

        JLabel label_2 = new JLabel("请输入"+DataInit.inventoryColumnNames.get(2)+"：");
        label_2.setHorizontalAlignment(SwingConstants.RIGHT);
        label_2.setBounds(67, 133, 139, 33);
        contentPane.add(label_2);

        JLabel label_3 = new JLabel("请输入"+DataInit.inventoryColumnNames.get(3)+"：");
        label_3.setHorizontalAlignment(SwingConstants.RIGHT);
        label_3.setBounds(99, 191, 107, 33);
        contentPane.add(label_3);

        JLabel label_4 = new JLabel("请输入"+DataInit.inventoryColumnNames.get(4)+"：");
        label_4.setHorizontalAlignment(SwingConstants.RIGHT);
        label_4.setBounds(53, 248, 153, 33);
        contentPane.add(label_4);

        JLabel label_5 = new JLabel("请输入"+DataInit.inventoryColumnNames.get(5)+"：");
        label_5.setHorizontalAlignment(SwingConstants.RIGHT);
        label_5.setBounds(99, 302, 107, 33);
        contentPane.add(label_5);

        textField = new JTextField();
        textField.setBounds(226, 24, 183, 27);
        contentPane.add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(226, 78, 183, 27);
        contentPane.add(textField_1);

        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(226, 136, 183, 27);
        contentPane.add(textField_2);

        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(226, 194, 183, 27);
        contentPane.add(textField_3);

        textField_4 = new JTextField();
        textField_4.setColumns(10);
        textField_4.setBounds(226, 251, 183, 27);
        contentPane.add(textField_4);

        textField_5 = new JTextField();
        textField_5.setColumns(10);
        textField_5.setBounds(226, 305, 183, 27);
        contentPane.add(textField_5);

        JButton button = new JButton("添加");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text = textField.getText();
                String text1 = textField_1.getText();
                String text2 = textField_2.getText();
                String text3 = textField_3.getText();
                String text4 = textField_4.getText();
                String text5 = textField_5.getText();
                boolean isFlag = false;
                if(text.equals("")){
                    JOptionPane.showMessageDialog(null, DataInit.inventoryColumnNames.get(0)+"不能为空", "提示",JOptionPane.WARNING_MESSAGE);
                    isFlag = true;
                }
                if(text1.equals("")){
                    JOptionPane.showMessageDialog(null, DataInit.inventoryColumnNames.get(1)+"不能为空", "提示",JOptionPane.WARNING_MESSAGE);
                    isFlag = true;
                }
                if(text2.equals("")){
                    JOptionPane.showMessageDialog(null, DataInit.inventoryColumnNames.get(2)+"不能为空", "提示",JOptionPane.WARNING_MESSAGE);
                    isFlag = true;
                }
                if(text3.equals("")){
                    JOptionPane.showMessageDialog(null, DataInit.inventoryColumnNames.get(3)+"不能为空", "提示",JOptionPane.WARNING_MESSAGE);
                    isFlag = true;
                }
                if(text4.equals("")){
                    JOptionPane.showMessageDialog(null, DataInit.inventoryColumnNames.get(4)+"不能为空", "提示",JOptionPane.WARNING_MESSAGE);
                    isFlag = true;
                }
                if(text5.equals("")){
                    JOptionPane.showMessageDialog(null, DataInit.inventoryColumnNames.get(5)+"不能为空", "提示",JOptionPane.WARNING_MESSAGE);
                    isFlag = true;
                }
                if(isFlag){
                    return;
                }
                InventoryEntity inventoryEntity=new InventoryEntity(text,text1,text2,text3,text4,text5);
                XMLFWriter.addInventoryEntity(inventoryEntity);
                JOptionPane.showMessageDialog(null, "恭喜你，添加成功.", "提示",JOptionPane.PLAIN_MESSAGE);
                Vector<Vector<String>> data = XMLFReader.getData(DataInit.TYPE_INVENTORY);
                MainUI.updateMainUI(DataInit.TYPE_INVENTORY,data,1);
                dispose();
            }
        });
        button.setBounds(226, 379, 93, 23);
        contentPane.add(button);
        setLocationRelativeTo(null);
        setVisible(true);
    }
}

